package com.authine.cloudpivot.sso.demo;

import com.authine.cloudpivot.engine.api.model.organization.UserModel;
import com.authine.cloudpivot.web.api.domain.Constants;
import com.authine.cloudpivot.web.api.service.CheckUserService;
import com.authine.cloudpivot.web.sso.template.ResponseTypeEnum;
import com.authine.cloudpivot.web.sso.template.SimpleOAuth2Template;
import com.authine.cloudpivot.web.sso.template.SimpleTemplateConfig;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.stereotype.Component;



@Component
@Slf4j
public class WeChatSecurityLogin extends SimpleOAuth2Template {

    @Autowired
    private CheckUserService checkUserService;



    WeChatSecurityLogin() {

        super(SimpleTemplateConfig.builder()
                .requestMatcher(new AntPathRequestMatcher("/login/wechat", HttpMethod.GET.name()))
                //验证通过后选择通过回调还是直接返回带token json串的方式
                .responseType(ResponseTypeEnum.JSON)
                //选择回调的话需要添加loginPassredirectUrl 会回调http://127.0.0.1/oauth地址然后把SSO的code传过去   如http://127.0.0.1/oauth?code=pOQrft    需要前端拿code获取SSO的token
//                .loginPassredirectUrl("http://127.0.0.1:8888/oauth/authorize?client_id=api&response_type=code&scope=read&redirect_uri=http://127.0.0.1/oauth")  //？
                .mobile(true)       //是否手机登录
                .redirectClientID("api")    //云枢OAuth2登录的ClientId  具体体现在base_security_client表中
                .build());
    }

    /**
     * 调用微信接口获取accessToken
     *   https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ID&corpsecret=SECRET
     *   corpid ：配置在 Constants.WECHAT_CORPID
     *   corpsecret： Constants.WECHAT_SERCRET
     * @param code
     * @return
     * @throws Exception
     */
    @Override
    public String getAccessToken(String code) throws Exception {
        String  token=checkUserService.getAccessToken(code, Constants.WECHAT);
        return token;
    }

    /**
     * 获取调用微信接口获取企业微信和云枢的关联字段userId，根据企业微信返回的email去云枢查询有没有对应用户
     *  微信接口URL：https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=ACCESS_TOKEN&code=CODE
     *  code： 通过成员授权获取到的code，最大为512字节。每次成员授权带上的code将不一样，code只能使用一次，5分钟未被使用自动过期。
     *  API ：https://work.weixin.qq.com/api/doc#90000/90135/91023
     * @param code
     * @return
     * @throws Exception
     */
    @Override
    public String getSourceId(String code) throws Exception {
        String sourceId=checkUserService.getSourceId(code);
        return sourceId;
    }

    @Override
    public UserModel getUser(Object sourceId) {
        return this.dubboConfigService.getOrganizationFacade().getUser(String.valueOf(sourceId));
    }





}